package com.flying.carclub.http;

import android.text.TextUtils;


import com.flying.carclub.MyApplication;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xcheng on 2016/3/23.
 */
public class OkRequestParams {
    private String charset = "UTF-8";
    public static int DEFAULT_TIME_OUT = 8000;  //ms
    private static String EXTRA_PARAMS_KEY_VERSION = "version";
    private String cookie;
    private String cookieKey;
    private String url;
    private int connectTimeOut = DEFAULT_TIME_OUT;
    //    private Map<String,String> params = new HashMap<>();
    private List<NameValuePair> params = new ArrayList<>();
    private List<NameValuePair> headers = new ArrayList<>();
    private String data;  //字符串数据
    private File file;

    public void setCharset(String charset) {
        if (!TextUtils.isEmpty(charset)) {
            this.charset = charset;
        }
    }

    public String getCharset() {
        return charset;
    }
    public OkRequestParams(String url) {
        this.url = url;
    }

    public void addQueryStringParameter(String key, String value) {
        if (!TextUtils.isEmpty(value)) {
            NameValuePair nameValuePair = new BasicNameValuePair(key, value);
            params.add(nameValuePair);
        }
    }
    public void addQueryStringParameterWithEmpty(String key, String value) {
            NameValuePair nameValuePair = new BasicNameValuePair(key, TextUtils.isEmpty(value)?"":value);
            params.add(nameValuePair);
    }

    public void addBodyParameter(String key, String value) {
        if (!TextUtils.isEmpty(value)) {
            NameValuePair nameValuePair = new BasicNameValuePair(key, value);
            params.add(nameValuePair);
        }
    }

    public void addAllBodyParameter(List<NameValuePair> nameValuePairs) {
        if (nameValuePairs != null && nameValuePairs.size() > 0) {
            params.addAll(nameValuePairs);
        }
    }
    public void addBodyParameterWithEmpty(String key, String value) {
        NameValuePair nameValuePair = new BasicNameValuePair(key, value);
        params.add(nameValuePair);
    }

    public List<NameValuePair> getHeaders() {
        return headers;
    }

    public void setHeaders(List<NameValuePair> headers) {
        this.headers = headers;
    }

    public void setConnectTimeout(int connectTimeOut) {
        this.connectTimeOut = connectTimeOut;
    }

    public int getConnectTimeout() {
        return connectTimeOut;
    }

    public String getCookie() {
        return cookie;
    }

    public void setHeader(String cookieKey, String cookie) {
        this.cookieKey = cookieKey;
        this.cookie = cookie;
        headers.clear();
        NameValuePair header = new BasicNameValuePair(cookieKey, cookie);
        headers.add(header);
    }

    public void addHeader(String cookieKey, String cookie) {
        this.cookieKey = cookieKey;
        this.cookie = cookie;
        NameValuePair header = new BasicNameValuePair(cookieKey, cookie);
        headers.add(header);
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public List<NameValuePair> getParams() {
        return params;
    }

    public void setParams(ArrayList<NameValuePair> params) {
        this.params = params;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
    public void setBodyContent(String data){
        this.data = data;
    }
    public String getBodyContent(){
        return this.data;
    }
    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public boolean isDefaultTimeOut() {
        return connectTimeOut == DEFAULT_TIME_OUT;
    }
}
